package org.nlp2rdf.navigator.component;

import org.apache.log4j.Logger;
import org.nlp2rdf.navigator.component.exception.ComponentInitException;

public abstract class Component implements IComponent {

	private static final Logger logger = Logger.getLogger(Component.class);

	protected IComponentConfig componentConfig = null;
	protected boolean init = false;

	public void setComponentConfig(IComponentConfig componentConfig) {
		this.componentConfig = componentConfig;
	}

	public IComponentConfig getComponentConfig() {
		if (componentConfig == null) {
			logger.warn("ComponentConfig for " + this.getClass().getSimpleName() + " was not set ");
		}
		return componentConfig;
	}

	public void init() throws ComponentInitException {
		_init();
		init = true;
		check();
		logger.debug("Initialized component " + componentConfig.getId() + " " + this.getClass().getSimpleName() + "");

	}

	/**
	 * A simple check. Logs a warning
	 * 
	 * @return true if everything is ok
	 */
	public boolean check() {
		boolean ret = true;
		if (componentConfig == null) {
			ret = false;
			logger.warn("ComponentConfig for " + this.getClass().getSimpleName() + " was not set ");
		}
		if (!init) {
			ret = false;
			logger.warn(componentConfig.getId() + " " + this.getClass().getSimpleName() + " was not initialized ");
		}
		
		return ret;
	}

	protected abstract void _init() throws ComponentInitException;

}
